package com.zcf.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.mapper.CollectMapper;
import com.zcf.pojo.Collect;
import com.zcf.pojo.Dining;
import com.zcf.pojo.User;
import com.zcf.utils.Hutool;
import com.zcf.utils.UploadImgUtils;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Controller
@RequestMapping("/collect")
@CrossOrigin
@ResponseBody
public class CollectController {

	@Autowired
	CollectMapper collectMapper;

	/**
	 * 收藏商家
	 */
	@PostMapping("addCollectLp")
	public Body addCollectLp(String userId, String diningId) {
		User user = new User();
		user.setId(userId);
		user = user.selectById();
		if (user == null) {
			return Body.newInstance("用户不存在");
		}
		Dining dining = new Dining();
		dining.setId(diningId);
		dining = dining.selectById();
		if (dining == null) {
			return Body.newInstance("商家不存在");
		}
		Collect collect = new Collect();
		collect.setDiningId(diningId);
		collect.setId(Hutool.getId());
		collect.setUserId(userId);
		collect.setDiningName(dining.getDiningName());
		collect.setDiningImage(dining.getImage());
		collect.setTime(Hutool.parseDateToString());
		Boolean falg = collect.insert();
		if (falg) {
			return Body.newInstance(collect);
		}
		return Body.BODY_401;
	}

	/**
	 * 取消收藏
	 */
	@PostMapping("deleteCollectlp")
	public Body deleteCollectlp(String userId, String diningId) {
		User user = new User();
		user.setId(userId);
		user = user.selectById();
		if (user == null) {
			return Body.newInstance("用户不存在");
		}
		Dining dining = new Dining();
		dining.setId(diningId);
		dining = dining.selectById();
		if (dining == null) {
			return Body.newInstance("商家不存在");
		}

		Collect collect = new Collect();
		EntityWrapper<Collect> ew = new EntityWrapper<>();
		ew.eq("user_id", userId);
		ew.eq("dining_id", diningId);
		collect = collect.selectOne(ew);
		if (collect != null) {
			Boolean flag = collect.deleteById();
			if (flag) {
				return Body.BODY_200;
			}
			return Body.BODY_451;
		}
		return Body.newInstance("没有收藏");
	}

	/**
	 * 收藏店铺
	 */
	@PostMapping("insertCollect")
	public Body insertCollect(Collect collect, MultipartFile[] file) throws Exception {
		User user = new User();
		EntityWrapper<User> wrapper = new EntityWrapper<>();
		wrapper.eq("user_id", collect.getUserId());
		user = user.selectOne(wrapper);
		if (user != null) {
			Dining dining = new Dining();
			dining.setId(collect.getDiningId());
			dining = dining.selectById();
			if (dining != null) {
				Collect collect2 = new Collect();
				collect2.setId(Hutool.getId());
				if (file != null && file.length > 0) {
					String heard = UploadImgUtils.uploadFiles(file);
					collect2.setDiningImage(heard);
				}
				collect2.setDiningId(collect.getDiningId());
				collect2.setPhone(collect.getPhone());
				collect2.setTime(Hutool.parseDateToString());
				collect2.setDiningName(collect.getDiningName());
				Boolean flag = collect2.insert();
				if (flag) {
					return Body.newInstance(1, "收藏成功");
				}
				return Body.newInstance(-1, "收藏失败");
			}
			return Body.newInstance(-1, "店铺不存在");
		}
		return Body.newInstance(-1, "用户不存在");
	}

	/**
	 * 取消收藏
	 */
	@PostMapping("deleteCollect")
	public Body deleteCollect(String id) {
		Collect collect = new Collect();
		collect.setId(id);
		Boolean flag = collect.deleteById();
		if (flag) {
			return Body.newInstance(1, "取消成功");
		}
		return Body.newInstance(-1, "取消失败");
	}

	/**
	 * 查询用户收藏列表
	 */
	@PostMapping("queryLieBiao")
	public Body queryLieBiao(String userId) {
		EntityWrapper<Collect> ew = new EntityWrapper<>();
		ew.eq("user_id", userId);
		ew.orderBy("time", false);
		List<Collect> lists = collectMapper.selectList(ew);
		for (Collect collect : lists) {
			Dining dining = new Dining();
			dining.setId(collect.getDiningId());
			dining = dining.selectById();
			if (!dining.getGrade().equals("")) {
				collect.setGrade(dining.getGrade());
				collect.setDiningImage(dining.getLogo());
			}
		}
		if (lists.size() > 0) {
			return Body.newInstance(lists);
		}
		return Body.newInstance("暂无收藏");
	}
}
